Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[download-microservice] Refactor: Clarify Execution, make bin finding clearer #132

Merged
merged 9 commits into from
Feb 4, 2024

Conversation

confused-Techie
Copy link
Member

This PR first addresses some great points @DeeDeeG brought up about code clarity, and aims to make things much more clear in behavior.

Secondly, since a refactor was already taking place, this PR removes likely the biggest portion of code. That is finding the bins. Previously this was a big long if...else which has no been replaced by a simple object, that allows easy lookup and definition of how to find each bin.

The ./bin.js file exports an object where the top level key is the OS for each supported platform, which itself then has a top level key to each type of supported installation on said platform. From there the following keys are supported to execute a check against the binary asset name:

  • startsWith: Executes string.startsWith(value)
  • endsWith: Executes string.endsWith(value)
  • endsWithNot: Executes !string.endsWith(value)

This way instead of repetitive code, with the definitions of how we find the binaries obscured within it, we instead have a single function decode this object into code with the clarity of the definitions being much more important.

@DeeDeeG
Copy link
Member

DeeDeeG commented Feb 2, 2024

Just going to plunk down a link to a branch I was working on, as I hadn't see you were working on this!

main...DeeDeeG:package-frontend:download-refactor-plus-more-error-handling

I can have a look at what you've got here as well, but maybe I shouldn't have suggested changes, bopped offline, gone in a meditative trance editing these files a ton, watched some media, eaten a meal or two, and then come back online several hours later to see you've already done it yourself!

@DeeDeeG
Copy link
Member

DeeDeeG commented Feb 2, 2024

The premise as per the PR description sounds really good, and kind of reaches all the things I wasn't bold enough to reach for as a contributor as opposed to original author. Probably gonna be pretty good, but I've been neck deep in the previous way of doing it for a few hours off and on, lol. So it'll take me a bit to absorb all these changes, but I think it's looking good at a first glance as well.

@confused-Techie
Copy link
Member Author

@DeeDeeG I gotta say, I really love the changes you have on your branch.
If you want we can build these off of each other, or I can simplify mine to only focus on the bin search changes, since that is what I care most about in this PR. Since otherwise I'd say your clarification and safety changes are much more thorough and functional than what I've got here

@DeeDeeG
Copy link
Member

DeeDeeG commented Feb 2, 2024

My favorite stuff in mine lives in index.js and might not conflict with your PR that much? That stuff can be slotted in independently of my changes in utils.js (thanks to the concerns being very cleanly separated by the original author I might point out) and I think your new approach by making all the binary info be data rather than directly as logic is really clean conceptually, so I might lean pretty heavily toward your stuff in utils.js and bins.js, as my first instinct here as I'm getting up to speed with your changes.

Reading as I go, lol.

Comment on lines +7 to +9
const url = req.url.split("?");
const path = url[0];
const queryParams = url[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep these const definitions and otherwise meld my changes of index.js into this PR it should apply pretty cleanly in terms of like diff conflicts and such.

Either this way or my branch are more readable than before, IMO. Your version here might be even slightly nicer, it's pretty subjective tho. path and queryParams seems very readable, IMO, as you have it on this branch already. 👍

Copy link
Member

@DeeDeeG DeeDeeG Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe too much thoughts on this, tl;dr I think your var names are better.

I ended up with this on my branch (snippet), which I wasn't in love with but couldn't think of how to get exactly the names I wanted and the readability I wanted:

  let [urlPath, urlParams] = req.url.split("?");

The destructuring assignment on my branch, while concise, is IMO more confusing and less readable.

And not much point in reminding where these consts came from (urlPath and urlParams) by keeping "url" in their names, when path and queryParams are so descriptive, without fluff and readable.

So that's my thoughts why your variable names/assignment approach works better, IMO.


EDIT to add: I can draft a patch and offer it for your consideration to merge my favorite bits of the two versions of index.js if you haven't already done it?

EDIT 2: We discussed it on Discord. This became #133 targeting for inclusion in this PR's branch directly.

@DeeDeeG
Copy link
Member

DeeDeeG commented Feb 2, 2024

Also, I have a small thing on utils.js that helps dev mode work, if an auth token 12345 is supplied, GitHub will reject it with an error, so I delete the Auth token bearer header thing in "dev mode":

if (process.env.PULSAR_STATUS === "dev") {
// We don't expect to be authed in dev mode.
// Fetching releases from GitHub without authentication is fine in dev mode.
delete options.headers['Authorization'];
}

Would love to see something along those lines to be able to test the microservice without auth some way.

UPDATE: Fixed in #133 which was merged into this PR's branch.

Copy link
Member

@DeeDeeG DeeDeeG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, with or without my changes, this bins.js thing and stringMatchesBin() separates things off a little more neatly than before, one might say.

It's less centralized, but I think each file is now more focused.

Also, things are labeled clearly with somewhat intuitive names, which I dunno about other folks, but that is a big one for me. Sometimes I come back to a project after weeks or months and it's like new code again for me. "Why was this set up like this? I forget..." Having very readable code in our "hopefully it just works" workhorse infrastructure is a great thing, IMO.

Even if this isn't the most dead simple way to possibly do it, it's one of the more understandable to read, when it comes down to it, and looks very "ergonomic" to use now, if working around in this code, and frankly I hope/expect it may not need much changes assuming we remain using GitHub Actions for the Rolling bins for the near future.

All that to say... looks good to me! 👍

Copy link
Member

@DeeDeeG DeeDeeG Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I have to admit, the big diff makes this seem like a big change, but really it's pretty much splitting off the data about what the OSes/types are, and how to string manipulate the bin URL to verify it, off into bins.js and some logic to step through that to use the data in the new stringMatchesBin() function.

So as is so often the case with "move stuff around, reformat it" things (i.e. "refactoring," like the PR title, go figure)... we get a big diff for doing the same thing a different way.

No problem, and makes findLink() (the main user of stringMatchesBin()) a lot cleaner and shorter. And separates the stringMatchesBin() off as its own utility function, which I like. (Looks nicer just reading the new code in an editor, without the distraction of the large diff in the diff view.)

Copy link
Member

@DeeDeeG DeeDeeG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as mentioned on Discord, I'm requesting changes from myself, namely I committed as [email protected], which is not my proper commit email.

BRB while I fix that.

Noting that the PR branch is currently at commit 214adb2, which I have backed up at https://github.com/DeeDeeG/package-frontend/commits/PR-132-backup-before-rebase.

Sorry about that!

DeeDeeG and others added 4 commits February 3, 2024 18:32
Neither risks much DDoS exposure, and this lets us update parameters
in the future without having to remember this limit is here and update
the limit as well.

Prevents us from accidentally breaking the microservice, although
the error messages if this got in our way would be extremely specific!

But not much downside to increasing this a bit, either.
Also, give an error that's now a plain '500' a more specific message.
DeeDeeG's February 2024 refactor of the Download microservice (complementary to PR 132)
Copy link
Member

@DeeDeeG DeeDeeG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good now!

@confused-Techie
Copy link
Member Author

Woot, thanks for everything on this one @DeeDeeG
Lets get it merged and pushed to prod!

@DeeDeeG DeeDeeG merged commit 6d650f7 into main Feb 4, 2024
1 check passed
@confused-Techie confused-Techie deleted the download/refactor branch February 4, 2024 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants